package com.surfilter.mq.rocketmq.impl;

import com.surfilter.mq.rocketmq.OprateMessageService;
import com.surfilter.mq.rocketmq.cache.RockMqMesgManage;
import com.surfilter.mq.rocketmq.cache.RocketMqMessageStandard;
import com.surfilter.mq.util.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


public class OprateMessageBlockServiceCache implements OprateMessageService {
private static Logger logger = LoggerFactory.getLogger(OprateMessageBlockServiceCache.class);
	
	private RockMqMesgManage manage;
	
	public OprateMessageBlockServiceCache(RockMqMesgManage manage){
		this.manage = manage;
	}

	@Override
	public void oprate(List<byte[]> messages) {
		// TODO Auto-generated method stub
		
		if(messages == null || messages.isEmpty()){
			return;
		}
		for(byte[] byte_message : messages){
			try {
				Object obj = ByteUtils.deserialize(byte_message);
				if(obj instanceof RocketMqMessageStandard){
					RocketMqMessageStandard rmsd = (RocketMqMessageStandard)obj;
					logger.info("接收到任务ID:{}", rmsd.getMesgKey());
					manage.addBlockQueue(rmsd);
				}else{
					logger.error("接收到非对称消息:{}", obj);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("格式化对象异常", e);
			}
		}
	}
}
